/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.payment.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.pitop.payment.api.PaymentTermFeignApi;
import com.pitop.core.exceptions.PaymentTermException;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.Page;
import com.pitop.payment.entity.PaymentTerm;


/**
 * 支付方式
 *
 * @author berton
 */
@Service("paymentTermSVImpl")
@Slf4j
public class PaymentTermSVImpl {

    @Autowired
    private PaymentTermFeignApi paymentTermFeignApi;

    /**
     * 加载一个对象PaymentTerm
     *
     * @param id * @param code 编码
     * @return PaymentTerm
     */
    public PaymentTerm load(Long id, String code) {
        if (id == null && code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return paymentTermFeignApi.load(id, code);
    }

    /**
     * 加载一个对象PaymentTerm 通过id
     *
     * @param id
     * @return PaymentTerm
     */
    public PaymentTerm loadById(Long id) {
        if (id == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return paymentTermFeignApi.loadById(id);
    }

    /**
     * 加载一个对象PaymentTerm 通过code
     *
     * @param code 编码
     * @return PaymentTerm
     */
    public PaymentTerm loadByCode(String code) {
        if (code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return paymentTermFeignApi.loadByCode(code);
    }


    /**
     * 删除对象PaymentTerm
     *
     * @param id * @param code 编码
     * @return PaymentTerm
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        paymentTermFeignApi.delete(id, code);
    }


    /**
     * 查询PaymentTerm分页
     *
     * @param paymentTerm 对象
     * @param curPage     当前页
     * @param pageSize    分页条数
     * @return List<PaymentTerm>
     */
    public List<PaymentTerm> list(PaymentTerm paymentTerm, int curPage, int pageSize) {
        return paymentTermFeignApi.list(paymentTerm, curPage, pageSize);
    }

    /**
     * 查询PaymentTerm分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<PaymentTerm>
     */
    public List<PaymentTerm> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        PaymentTerm paymentTerm = new PaymentTerm();
        paymentTerm.setId(id);
        paymentTerm.setCode(code);

        return paymentTermFeignApi.list(paymentTerm, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return paymentTermFeignApi.count(id, code);
    }


    public int count(PaymentTerm paymentTerm) {
        return paymentTermFeignApi.count(paymentTerm);
    }


    /**
     * 保存
     * 1.验证数据合法性
     * ****数据非空判断
     * ****支付方式是否已经被使用
     * 2.保存支付方式
     *
     * @param paymentTerm 实体
     * @throws BaseException
     */
    public void save(PaymentTerm paymentTerm) {
        //1.验证数据合法性
        if (paymentTerm == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //支付方式是否已经被使用
        PaymentTerm paymentTermFlag = paymentTermFeignApi.loadByPayment(paymentTerm.getPayment());
        if (paymentTermFlag != null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.保存支付方式
        paymentTermFeignApi.build(paymentTerm);
    }

    /**
     * 更新
     * 1.验证数据合法性
     * ****数据非空判断
     * ****支付方式是否已经被使用
     * 2.更新支付方式
     *
     * @param paymentTerm 实体
     * @throws BaseException
     */
    public void modify(PaymentTerm paymentTerm) {
        //1.验证数据合法性
        if (paymentTerm == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //支付方式是否已经被使用
        PaymentTerm paymentTermFlag = paymentTermFeignApi.loadByPayment(paymentTerm.getPayment());
        if (paymentTermFlag != null && !paymentTermFlag.getCode().equals(paymentTerm.getCode())) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.更新支付方式
        paymentTermFeignApi.modify(paymentTerm);
    }

}
